package cn.bdqn.t354.controller;

import cn.bdqn.t354.pojo.Dict;
import cn.bdqn.t354.service.DictService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/dict")
public class DictController {
    @Resource
    private DictService dictService;

    /**
     * 列表展示
     *
     * @param dictType
     * @param dictItem
     * @param pageIndex
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(String dictType, String dictItem,
                       @RequestParam(defaultValue = "1") Integer pageIndex,
                       Model model) {
        //设置每页显示条数
        int pageSize = 3;

        //查询每页显示的数据
        IPage<Dict> dictPager =
                dictService.getDictsByPage(dictType, dictItem, pageIndex, pageSize);
        model.addAttribute("dictPager", dictPager);

        //查询条件回显
        model.addAttribute("dictType", dictType);
        model.addAttribute("dictItem", dictItem);

        return "dict/list";
    }

    /**
     * 跳转到新增页面
     *
     * @return
     */
    @RequestMapping("/add")
    public String add() {
        return "dict/add";
    }

    /**
     * 新增操作
     *
     * @param dict
     * @return
     */
    @RequestMapping("/save")
    public String save(Dict dict) {
        try {
            dictService.addDict(dict);
            //跳转到列表页面
            return "redirect:/dict/list";
        } catch (Exception e) {
            e.printStackTrace();
            //跳转到新增页面
            return "redirect:/dict/add";
        }
    }

    /**
     * 跳转到修改页面
     *
     * @param dictId
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public String edit(Long dictId, Model model) {
        //1.根据id查询数据字典
        Dict dict = dictService.getDict(dictId);
        model.addAttribute("dict", dict);

        return "dict/edit";
    }

    /**
     * 修改操作
     *
     * @param dict
     * @return
     */
    @RequestMapping("/update")
    public String update(Dict dict) {
        try {
            dictService.updateDict(dict);
            //跳转到列表页面
            return "redirect:/dict/list";
        } catch (Exception e) {
            e.printStackTrace();
            //跳转到修改页面
            return "redirect:/dict/edit";
        }
    }

    /**
     * 删除操作
     *
     * @param dictId
     * @param model
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(Long dictId, Model model) {
        Map<String, Object> map = new HashMap<>();
        try {
            dictService.deleteDict(dictId);
            map.put("delResult", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("delResult", false);
        }
        return map;
    }
}
